if (typeof $ === 'function') {
    $(function () {
        var HFAlert = {
            defaultConfig: {
                title:'',
                message:'',
                width: 320,
                height: 170,
                timer: 0,
                type: 'warning',
                showOverlay:true,//是否显示背景遮罩（默认：true）
                popMaskClose:false,//是否可以点击遮罩层关闭 (默认：false)
                autoHide:true,//自动关闭（默认：false）
                autoHideMsecond:4000,//自动关闭时间，改参数需要autoHide为true
                showConfirmButton: true,
                showCancelButton: false,
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                callback: null,
                cancelCallback:null
            },
            html: '<div class="HFAlert_box">' +
            '<div class="HFAlert_image"></div>' +
            '<div class="HFAlert_title"></div>' +
            '<div class="HFAlert_message"></div>' +
            '<div class="HFAlert_button">' +
            '<button class="HFAlert_cancel"></button>' +
            '<button class="HFAlert_confirm"></button>' +
            '</div>' +
            '</div>',
            overlay: '<div class="HFAlert_overlay"></div>',
            open: function (o) {
                var opts = {}, that = this;
                $.extend(opts, that.defaultConfig, o);
                $('body').append(that.html);
                if(opts.showOverlay){
                	$('body').append(that.overlay)
                }
                var box = $('.HFAlert_box');
                box.css({
                    'width': opts.width + 'px',
                    'min-height': opts.height + 'px',
                    'margin-left': -(opts.width / 2) + 'px'
                });
                $('.HFAlert_image').addClass(opts.type);
                opts.title && $('.HFAlert_title').html(opts.title).show(),
                opts.message && $('.HFAlert_message').html(opts.message).show();
                var confirmBtn = $('.HFAlert_confirm'),
                    cancelBtn = $('.HFAlert_cancel');
                opts.showConfirmButton && confirmBtn.text(opts.confirmButtonText).show(),
                opts.showCancelButton && cancelBtn.text(opts.cancelButtonText).show();
                opts.popMaskClose && $('.HFAlert_overlay').unbind('click').bind('click', function () {
                    // console.log("popMaskClose"+typeof opts.callback === 'function' && opts.callback());
                    that.close();
                });
                confirmBtn.unbind('click').bind('click', function () {
                    // console.log("confirmBtn"+typeof opts.callback === 'function' && opts.callback());
                    that.close();
                    typeof opts.callback === 'function' && opts.callback();
                });
                cancelBtn.unbind('click').bind('click', function () {
                    // console.log("cancelBtn"+typeof opts.callback === 'function' && opts.callback());
                    that.close();
                    typeof opts.cancelCallback === 'function' && opts.cancelCallback();
                });
                var h = box.height();
                box.css({
                    'margin-top': -(Math.max(h, opts.height) / 2 + 100) + 'px'
                });
                if(opts.autoHide){
                	window.setTimeout("hideOverlay()",opts.autoHideMsecond);	
                }
            },
            close: function () {
                $(".HFAlert_overlay,.HFAlert_box").remove();
            }
        };
        window.hideOverlay = function(){
        	$(".HFAlert_overlay,.HFAlert_box").remove();
        };
        window.HFalert = function (opts) {
            HFAlert.open(opts);
        };
        window.HFconfirm = function (opts) {
            opts = $.extend({showCancelButton: true}, opts);
            if(!opts.type){
            	opts.type = 'question'; 
            }
            if (typeof opts.callback === 'function') {
                // console.log("cancelBtn11111");
                HFAlert.open(opts);
            }
        }
    });
}
